/*
 *  $Id: Main.cs 1199 2010-11-20 17:38:24Z thenn.erannor $
 *  This file is a part of Ragima CCG.
 *  (C) 2008 Ragima development group.
 *
 *  Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

using AgateLib;
using AgateLib.BitmapFont;
using AgateLib.DisplayLib;
using AgateLib.Geometry;
using AgateLib.Resources;

using Ragima.Languages;

namespace Ragima.ResourceGenerator {
    class MainClass {
        private const string AppName = "Resource Generator";

        public static void Main(string[] args) {
            Language.AllLanguages.AddLanguages(Paths.Languages, true);

            Console.WriteLine(AppName);

            using (var setup = new AgateSetup(AppName, args)) {
                setup.AskUser = false;
                try {
                    setup.InitializeDisplay();
                } catch (Exception e) {
                    ErrorReport(e, true);
                    return;
                }
                if (setup.WasCanceled)
                    return;
                
                Core.AutoStackTrace = true;

                var wParams = new CreateWindowParams();
                wParams.Title = AppName;
                wParams.Size = new Size(100, 100);
                wParams.WindowPosition = WindowPosition.CenterScreen;
                new DisplayWindow(wParams);

                foreach (var lang in Language.AllLanguages.GetLanguages())
                    GenerateFonts(lang);
            }
        }

        public static void GenerateFonts(Language lang) {
            Console.WriteLine("Generate fonts for lang: {0}", lang.LangCode);
            var fonts = FontInfoCollection.Load(Paths.Data[lang.GetFontsFileName()]);
            var resources = new AgateResourceCollection();
            foreach (var info in fonts.GetFonts()) {
                var res = GenerateFont(lang, info.Key, info.Value);
                if (res != null)
                    resources.Add(res);
            }
            var resPath = Paths.Data[lang.GetResourcesFileName()];
            AgateResourceLoader.SaveResources(resources, resPath);
        }

        public static AgateResource GenerateFont(Language lang, string id, BitmapFontOptions options) {
            Console.WriteLine("Generate font: {0}", id);
            var font = new FontSurface(options);
            var impl = font.Impl as BitmapFontImpl;
            if (impl == null) {
                Console.WriteLine("Failed");
                return null;
            }
            var resName = lang.GetFontId(id);
            var imageName = string.Format("{0}.png", resName);
            var imagePath = Paths.Fonts[imageName];
            var res = new BitmapFontResource(resName);
            impl.Surface.SaveTo(imagePath);
            res.Image = imageName;
            res.FontMetrics = impl.FontMetrics;
            return res;
        }

        public static void ErrorReport(Exception e, bool showMessage) {
            Console.WriteLine("Error: ({0}) {1}", e.GetType().Name, e.Message);
            Console.WriteLine(e.StackTrace);
            using (var wr = new StreamWriter("error.log", true)) {
                wr.WriteLine("Date: {0}", DateTime.Now);
                wr.WriteLine("Error: ({0}) {1}", e.GetType().Name, e.Message);
                wr.WriteLine(e.StackTrace);
                wr.WriteLine();
            }
            if (showMessage)
                MessageBox.Show("Error saved to 'error.log' file.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

